package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.base.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("house")
public class HouseController extends BaseController {
    @Reference
    private HouseService houseService;
    @Reference
    private DictService dictService;
    @Reference
    private CommunityService communityService;
    @Reference
    private HouseImageService houseImageService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private HouseUserService houseUserService;


    @RequestMapping
    public String index(HttpServletRequest request, Model model){
        Map<String, Object> filters = getFilters(request);
        model.addAttribute("filters",filters);
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page",page);
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);
        List<Dict> houseTypeList = dictService.findListByCode("houseType");
        model.addAttribute("houseTypeList",houseTypeList);
        List<Dict> floor = dictService.findListByCode("floor");
        model.addAttribute("floorList",floor);
        List<Dict> buildStructure = dictService.findListByCode("buildStructure");
        model.addAttribute("buildStructureList",buildStructure);
        List<Dict> direction = dictService.findListByCode("direction");
        model.addAttribute("directionList",direction);
        List<Dict> decoration = dictService.findListByCode("decoration");
        model.addAttribute("decorationList",decoration);
        List<Dict> houseUse = dictService.findListByCode("houseUse");
        model.addAttribute("houseUseList",houseUse);

        return "house/index";
    }

    @GetMapping("create")
    public String create(Model model){
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);
        List<Dict> houseTypeList = dictService.findListByCode("houseType");
        model.addAttribute("houseTypeList",houseTypeList);
        List<Dict> floor = dictService.findListByCode("floor");
        model.addAttribute("floorList",floor);
        List<Dict> buildStructure = dictService.findListByCode("buildStructure");
        model.addAttribute("buildStructureList",buildStructure);
        List<Dict> direction = dictService.findListByCode("direction");
        model.addAttribute("directionList",direction);
        List<Dict> decoration = dictService.findListByCode("decoration");
        model.addAttribute("decorationList",decoration);
        List<Dict> houseUse = dictService.findListByCode("houseUse");
        model.addAttribute("houseUseList",houseUse);
        return "house/create";
    }

    @PostMapping("save")
    public String save(House house){
        houseService.insert(house);
        return "common/successPage";
    }
    @GetMapping("edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        House house = houseService.getById(id);
        model.addAttribute("house",house);
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);
        List<Dict> houseTypeList = dictService.findListByCode("houseType");
        model.addAttribute("houseTypeList",houseTypeList);
        List<Dict> floor = dictService.findListByCode("floor");
        model.addAttribute("floorList",floor);
        List<Dict> buildStructure = dictService.findListByCode("buildStructure");
        model.addAttribute("buildStructureList",buildStructure);
        List<Dict> direction = dictService.findListByCode("direction");
        model.addAttribute("directionList",direction);
        List<Dict> decoration = dictService.findListByCode("decoration");
        model.addAttribute("decorationList",decoration);
        List<Dict> houseUse = dictService.findListByCode("houseUse");
        model.addAttribute("houseUseList",houseUse);
        return "house/edit";
    }

    @PostMapping("update")
    public String update(House house){
        houseService.update(house);
        return "common/successPage";
    }

    @GetMapping("delete/{id}")
    public String delete(@PathVariable Long id){
        houseService.delete(id);
        return "redirect:/house";
    }

    @RequestMapping("publish/{id}/{status}")
    public String publish(@PathVariable Long id,@PathVariable Integer status){
        houseService.publish(id,status);
        return "redirect:/house";
    }

    @RequestMapping("{id}")
    public String detail(@PathVariable Long id,Model model){
        House house = houseService.getById(id);
        model.addAttribute("house",house);
        Community community = communityService.getById(house.getCommunityId());
        model.addAttribute("community",community);
        //获取当前房源的房源图片信息
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
        model.addAttribute("houseImage1List",houseImage1List);
        List<HouseImage> houseImage2List = houseImageService.findList(id, 2);
        model.addAttribute("houseImage2List",houseImage2List);
        //获取当前经纪人的信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        model.addAttribute("houseBrokerList",houseBrokerList);
        //获取当前房源的房东信息
        List<HouseUser> houseUserList = houseUserService.findListByHouseId(id);
        model.addAttribute("houseUserList",houseUserList);
        return "house/show";
    }
}
